package org.i1510.common.interceptor;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i1510.common.utils.KBProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SessionOutInterceptor extends HandlerInterceptorAdapter {


	private static Logger logger = LoggerFactory.getLogger(SessionOutInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HandlerMethod handlerM =(HandlerMethod) handler;
      
        String requestUri = request.getRequestURI();  
        String contextPath = request.getContextPath();  
        //得到访问地址  
        String url = requestUri.substring(contextPath.length());  
          

		HttpSession session = request.getSession();
		Object userSession = session.getAttribute("userSession");
        //request.getRequestDispatcher(request.getContextPath()+"/login/login.jsp").forward(request, response);  
        //response.sendRedirect(request.getContextPath()+"/login/login.jsp");  
		if (null == userSession) { //未登录
            if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有，x-requested-with  
                response.setHeader("sessionstatus", "timeout");//在响应头设置session状态  
            }else{
            	if(KBProperties.STARTSYS == 1){
            		response.sendRedirect(request.getContextPath()+"/joeone/ipad/index_dhVote.jsp");
            	}else if(KBProperties.STARTSYS == 0){
            		response.sendRedirect(request.getContextPath()+"/joeone/ipad/index.jsp");
            	}
                
            }  
            return false;
        }
        return true;
    }

}